/*
 * VerDetalleRecibo.java
 *
 * Created on 27 de marzo de 2007, 22:35
 */

package vista;

import controlador.DetalleFactura;
import controlador.DetalleRecibo;
import controlador.Factura;
import controlador.Recibo;
import java.util.LinkedList;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  PC
 */
public class VerDetalleRecibo extends javax.swing.JFrame {
    
   private Recibo recibo;
   /** Creates new form VerDetalleRemito */
   public VerDetalleRecibo(Recibo rec) {
        this.recibo= rec;
       
        initTabla();
        initComponents();
        numero.setText(recibo.getNumero());
        fecha.setText(recibo.getFecha());
        cliente.setText(recibo.getCliente().getNombre());
        setTotales();
        this.setTitle("Recibo nro " + recibo.getNumero() + " de " + recibo.getCliente().getNombre());    
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        numero = new javax.swing.JLabel();
        fecha = new javax.swing.JLabel();
        cliente = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        labelBruto = new javax.swing.JLabel();
        labelDescuento = new javax.swing.JLabel();
        labelNeto = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jTable1.setModel(getModel());
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setText("Numero:");

        jLabel2.setText("Fecha:");

        jLabel3.setText("Cliente:");

        numero.setText("jLabel4");

        fecha.setText("jLabel5");

        cliente.setText("jLabel6");

        jButton1.setText("Cerrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel4.setText("Total descuento:");

        jLabel5.setText("Total NETO: ");

        jLabel6.setText("Total bruto:");

        jLabel7.setText("$");

        jLabel8.setText("$");

        jLabel9.setText("$");

        labelBruto.setText("jLabel10");

        labelDescuento.setText("jLabel11");

        labelNeto.setText("jLabel12");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(413, 413, 413)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel7))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelBruto, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(labelNeto, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                                    .addComponent(labelDescuento, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(fecha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(numero, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(numero))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(fecha))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cliente))
                .addGap(19, 19, 19)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelBruto)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelDescuento)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelNeto))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel9))))
                .addGap(22, 22, 22)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    
    public void setTotales(){
        float neto = 0;
        float bruto = 0;
        float descuento = 0;
            
        for(int i = 0; i < dtm.getRowCount(); i++){
            bruto += (Float)dtm.getValueAt(i, 5);
            descuento += (Float)dtm.getValueAt(i, 7);
            neto += (Float)dtm.getValueAt(i, 8);
        }
        labelBruto.setText(String.valueOf(bruto));
        labelDescuento.setText(String.valueOf(descuento));
        labelNeto.setText(String.valueOf(neto));
    }
    public DefaultTableModel getModel(){
        return this.dtm;
    } 
    
    public void initTabla(){
        dtm = new DefaultTableModel();
        dtm.addColumn("Factura");
        dtm.addColumn("Cod");
        dtm.addColumn("Titulo");
        dtm.addColumn("Precio");
        dtm.addColumn("Cant.");
        dtm.addColumn("Bruto");
        dtm.addColumn("% Comision");
        dtm.addColumn("Descuento");
        dtm.addColumn("Subtotal");
        
        LinkedList detalles = recibo.getDetalles();
        for(int i = 0; i < detalles.size(); i++){
            DetalleRecibo dr = (DetalleRecibo) detalles.get(i);
            dtm.addRow(getFila(dr));
        }
        detalles.clear();        
    }
    
    private Object[] getFila(DetalleRecibo dr){
        Factura factura = new DetalleFactura(dr.getIdDetalleFactura()).getFactura();
        float bruto = (dr.getEjemplar().getPrecioUnitario() * dr.getCantidad());
        float desc = bruto * (factura.getComision() / 100);
        float subtot = bruto - desc;
        
        Object[] datos = new Object[9];
        datos[0] = factura.getTipo() + " " + factura.getNumero();
        datos[1] = dr.getEjemplar().getLibro().getId();
        datos[2] = dr.getEjemplar().getLibro().getTitulo();
        datos[3] = dr.getEjemplar().getPrecioUnitario();
        datos[4] = dr.getCantidad(); 
        datos[5] = bruto;
        datos[6] = factura.getComision();
        datos[7] = desc;
        datos[8] = subtot;
        
        return datos;
    }
    public void insertarFila(DetalleRecibo dr){
        this.dtm.addRow(getFila(dr));
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel cliente;
    private javax.swing.JLabel fecha;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel labelBruto;
    private javax.swing.JLabel labelDescuento;
    private javax.swing.JLabel labelNeto;
    private javax.swing.JLabel numero;
    // End of variables declaration//GEN-END:variables
    private DefaultTableModel dtm;
}
